package com.zhangln.mountbuzhou.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhangln.mountbuzhou.config.MQConstant;
import com.zhangln.mountbuzhou.dao.TbOnlineDao;
import com.zhangln.mountbuzhou.dto.BaseResponse;
import com.zhangln.mountbuzhou.dto.ResultCode;
import com.zhangln.mountbuzhou.dto.request.PushMsgReq;
import com.zhangln.mountbuzhou.entity.TbOnlineEntity;
import com.zhangln.mountbuzhou.service.PushService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * @author sherry
 * @description
 * @date Create in 2020/4/27
 * @modified By:
 */
@RestController
@Slf4j
@RequestMapping("/msg")
public class MsgController {

    private final RabbitTemplate rabbitTemplate;
    private final PushService pushService;
    private final TbOnlineDao tbOnlineDao;

    public MsgController(RabbitTemplate rabbitTemplate, PushService pushService, TbOnlineDao tbOnlineDao) {
        this.rabbitTemplate = rabbitTemplate;
        this.pushService = pushService;
        this.tbOnlineDao = tbOnlineDao;
    }

    /**
     * 请求提交
     *
     * @param pushMsgReq
     * @return
     */
    @PostMapping("/push")
    public BaseResponse pushMsg(@Validated PushMsgReq pushMsgReq) {
        pushMsgReq.setMsgId(UUID.randomUUID().toString());

        log.info("推送请求：{}", pushMsgReq);

//        先判断一下当前请求有没有channel在线
        //            推送参数校验
        BaseResponse checkParam = pushService.checkPushParam(pushMsgReq.getPushId(), pushMsgReq.getArea()
                , pushMsgReq.getOrgCode(), pushMsgReq.getDevice(), pushMsgReq.getUserId());
        if (!checkParam.isSuccess()) {
            log.info(String.valueOf(checkParam));
            return checkParam;
        }

        List<TbOnlineEntity> tbOnlineEntities = tbOnlineDao.list(new QueryWrapper<TbOnlineEntity>()
                .eq(StringUtils.isNotEmpty(pushMsgReq.getArea()), TbOnlineEntity.AREA, pushMsgReq.getArea())
                .eq(StringUtils.isNotEmpty(pushMsgReq.getOrgCode()), TbOnlineEntity.ORG_CODE, pushMsgReq.getOrgCode())
                .eq(StringUtils.isNotEmpty(pushMsgReq.getDevice()), TbOnlineEntity.DEVICE, pushMsgReq.getDevice())
                .eq(StringUtils.isNotEmpty(pushMsgReq.getUserId()), TbOnlineEntity.USER_ID, pushMsgReq.getUserId())
        );
        if (CollectionUtils.isEmpty(tbOnlineEntities)) {
            return BaseResponse.builder().code(ResultCode.FAILURE).message("无满足条件的连接在线，本次推送无法完成").build();
        }

        rabbitTemplate.convertAndSend(MQConstant.WEB_SOCKET, MQConstant.PUSH, pushMsgReq);
        return BaseResponse.builder().code(ResultCode.SUCCESS).message("已收到推送请求").data(pushMsgReq.getMsgId()).build();
    }

    /**
     * 定时任务调度此方法，将客户端反馈成功的消息进行通知
     * 仅通知当天的结果，每批消息通知3次
     *
     * @return
     */
    @GetMapping("/callback")
    public BaseResponse noticeMsgPushResult() {
//        TODO
        /*
        新建一张表，去维护推送结果回调
         */
        return null;
    }

}
